﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using System.Collections;
using JEng_Mark_1.GameComponents;

namespace JEng_Mark_1.Managers
{
    public class JCameraManager : GameComponent
    {
        private static Hashtable _cameras = new Hashtable();

        public enum CameraNumber
        {
            _default = 1,
            _dolly = 2,
            _3 = 3,
            _4 = 4,
            _5 = 5,
            _6 = 6,
            _7 = 7,
            _8 = 8,
            _9 = 9,
            _10 = 10,
        }

        private static bool _initialized = false;

        /// <summary>
        /// Is the Camera Manager initialized?
        /// </summary>
        public static bool Initialized { get { return _initialized; } }

        private static JCamera _activeCamera;

        /// <summary>
        /// Get the currently active camera
        /// </summary>
        public static JCamera ActiveCamera { get { return _activeCamera; } }

        /// <summary>
        /// Construct the camera manager
        /// </summary>
        /// <param name="game">The XNA game object</param>
        public JCameraManager(Game game)
            : base(game)
        {
            Enabled = true;
        }

        /// <summary>
        /// Setup the default cameras
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();

            AddCamera(new JFirstPersonCamera(), CameraNumber._default);
            SetActiveCamera(CameraNumber._default);
            AddCamera(new JFirstPersonCamera(), CameraNumber._dolly);

            _initialized = true;
        }

        /// <summary>
        /// Update the active camera.
        /// </summary>
        /// <param name="gameTime">Timing snapshot</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            _activeCamera.Update(gameTime);
        }

        /// <summary>
        /// Adds a new camera to the manager
        /// </summary>
        /// <param name="newCamera">Camera to add</param>
        /// <param name="cameraNumber">camera reference number</param>
        public static void AddCamera(JCamera newCamera, CameraNumber cameraNumber)
        {
            if (!_cameras.Contains(cameraNumber))
            {
                _cameras.Add(cameraNumber, newCamera);
                JLog.addMessage("Added camera: " + newCamera.ToString() + " id = " + cameraNumber);
            }
        }

        /// <summary>
        /// Change the projection matrix of all cameras
        /// </summary>
        /// <param name="aspectRatio">Ratio of render window width to height</param>
        public static void SetAllCamerasProjectionMatrix(float aspectRatio)
        {
            foreach (JCamera camera in _cameras.Values)
            {
                camera.Projection = Matrix.CreatePerspectiveFieldOfView(camera.FieldOfView, aspectRatio, camera.NearPlane, camera.FarPlane);
            }
        }

        public static void SetActiveCamera(CameraNumber cameraNumber)
        {
            if (_cameras.Contains(cameraNumber))
            {
                _activeCamera = (_cameras[cameraNumber] as JCamera);
            }
        }
    }
}
